﻿using DefeatEpidemic.Common;
using Microsoft.AspNetCore.Mvc.Filters;
using System;

namespace DefeatEpidemic.Base
{
    public class AuthorizationFilterAttribute : Attribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            string controllerName = context.RouteData.Values["Controller"].ToString();//通过ActionContext类的RouteData属性获取Controller的名称：Home
            string actionName = context.RouteData.Values["Action"].ToString();//通过ActionContext类的RouteData属性获取Action的名称：Index

            string fullActionName = context.ActionDescriptor.DisplayName;//通过ActionContext类的ActionDescriptor属性，也可以获取Action的名称
            //不过这样获取到的是Action的完全限定名："AspNetCoreFilterContext.Controllers.HomeController.Index (AspNetCoreFilterContext)"，可以看到其中还包含Controller的类名、命名空间和程序集名称
            NLogUtil.fileLogger.Trace($"{context.HttpContext.Request.Host}.{fullActionName}");
            //var permission = new Models.AdminPermission()
            //{
            //    Title = "",
            //    Description = "",
            //    ActionName = actionName,
            //    ControllerName = controllerName
            //};
            //System.Threading.Tasks.Task.Run(() => DAL.PermissionDAL.AddPermission(permission));
        }
    }
}
